package net.sx.minemodule;

import com.alibaba.fastjson.JSON;
import com.homegoods.area.model.SubModelList;
import com.homegoods.classify.model.Constant;
import com.homegoods.resource.HomeGoodsApp;
import com.network.retrofit_rx.Api.ActionSubscriber;
import com.network.retrofit_rx.Api.CommandImpl;
import com.network.retrofit_rx.Api.HttpManager;


import net.sx.minemodule.mvp.bean.OrderListBean;
import net.sx.minemodule.mvp.bean.UpdatePwdParams;


import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author ch
 * @ClassName: ApiUtil
 * @date 2018/11/27
 */
public class MineApiUtils {
    /**
     * 修改用户信息
     *
     * @param fieldName  要修改的信息名称
     * @param fieldValue 信息内容
     * @param error
     */
    public static void modifyUserInfo(String fieldName, String fieldValue, Consumer<String> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_UPD)
                .addParams("token", HomeGoodsApp.getToken())
                .addField(fieldName, fieldValue)
                .setObject(true)
                .setClass(String.class)
                .create();
        HttpManager.getInstance().postUrlForQuery(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 修改用户手机号,修改登录密码，修改支付密码
     *
     * @param error
     */
    //del shibo.zheng start
    /*public static void modifyUserPhoneAndPwd(String fieldeName, String fieldValue, String code, Consumer<String> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_UPD)
                .addParams("token", HomeGoodsApp.getToken())
                .addField(fieldeName, fieldValue)
                .addField("SMSCode", code)
                .setObject(true)
                .setClass(String.class)
                .create();
        HttpManager.getInstance().postUrlForQuery(command, new ActionSubscriber<>(consumer, error, null));
    }*/   //del shibo.zheng end
    //add shibo.zheng start
    public static void modifyUserPhoneAndPwd(UpdatePwdParams params, Consumer<String> consumer, Consumer<Throwable> error) {
        HttpManager.getInstance().post(Constant.SERVER_URL + Constant.CLIENT_UPD,
                JSON.toJSONString(params),
                new ActionSubscriber<>(consumer, error, null),
                String.class);
    }
    //add shibo.zheng end

    /**
     * 文件上传前上报
     *
     * @param error lockid=200
     *              filesize=102400
     *              filename=829095120.jpg
     *              md5=35f48d1c5770e6cfc2916d5c13f23ba7
     *              usid=partner_087de3fe984a464a93199390f1edc9ee
     */
    public static void postKDSFile(String md5, String fileName, long fileSize, String usid, Consumer<String> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.AFU_KDSFFILE)
                .addParams("lockid", usid)
                .addParams("filesize", fileSize)
                .addParams("filename", fileName)
                .addParams("md5", md5)
                .addParams("usid", usid)
                .setObject(true)
                .setClass(String.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 文件上传数据体
     *
     * @param error --md5=35f48d1c5770e6cfc2916d5c13f23ba7--（文件md5值）
     *              必选项--tag=QQ图片20160905135128.jpg--（文件名）
     *              必选项--cmd=身份证图片--（文件描述）
     *              必选项--cls=附件应用--（分类）
     *              必选项--file=文件对象--（文件对象）
     */
    public static void postFile(String md5, String fileName, File file, Consumer<String> consumer, Consumer<Throwable> error) {
        Map<String, RequestBody> map = new HashMap<>();
        map.put("cmd", addParameter("头像"));
        map.put("cls", addParameter("附件应用》用户上传 》形像照片"));
        map.put("tag", addParameter(fileName));
        map.put("file\";  filename=\"" + file.getName(), addParameter(file));
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.AFU_KDSFFILE)
                .addParams("md5", md5)
                .putBodys(map)
                .setObject(true)
                .setClass(String.class)
                .create();
        HttpManager.getInstance().uploadfile(command, new ActionSubscriber<>(consumer, error, null));
    }

    public static RequestBody addParameter(Object o) {
        RequestBody body = null;
        if (o instanceof String) {
            body = RequestBody.create(MediaType.parse("multipart/form-data"), (String) o);
        } else if (o instanceof File) {
            body = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), (File) o);
        }
        return body;
    }

    /**
     * 获取我的收藏列表，商品收藏列表
     * token=b230e22fcf634cf8afb63e52bdf     //登录令牌
     * BindType=LoveCommodity                //统计收藏类别
     * BOWay=self$all
     * psize=50
     * pos=1
     */
    public static void getMyCollectionGoodsList(int pos, Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.DLIST_CENTER)
                .addParams("AppType", "commodity_")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("BindType", "LoveCommodity")
                .addParams("BOWay", "self")
                .addParams("pos", pos)
                .addParams("psize", "20")
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的收藏，设计师收藏列表
     *
     * @param error AppType=Mystylist_  //方法名 Bytype后面接接1获取商品收藏列表
     *              outdata=jsonlist       //数据返回格式
     *              token= pp              //用户token验证
     */
    public static void getMyCollectionStylistList(Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.DLISTCENTER)
                .addParams("AppType", "Mystylist_")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsonlist")
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的收藏，设计师收藏列表
     *
     * @param error AppType=Mystylist_  //方法名 Bytype后面接接1获取商品收藏列表
     *              outdata=jsonlist       //数据返回格式
     *              token= pp              //用户token验证
     *              TODO
     */
    public static void getMyCollectionVideo(Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.DLISTCENTER)
                .addParams("AppType", "Mystylist_")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsonlist")
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的订单列表
     *
     * @param error 参数名=参数值    //是否必选项及意义描述
     *              cmd=OrderList          //标识参数
     *              outdata=jsondata       //输出方式
     *              token=0000             //身份验证
     *              type=0    //1待付款 2待发货 3待收货 4确认收货 5待评论
     */
    public static void getMyOrderList(int type, Consumer<OrderListBean> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.GET_ORDER_LIST)
                .addParams("cmd", "OrderList")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsondata")
                .addParams("type", type)
                .setObject(true)
                .setClass(OrderListBean.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的订单列表
     *
     * @param error AppType=RefundList_ //固定传RefundList_
     *              token=0000              //传用户的token
     *              outdata=jsonlist         //数据输出方式
     */
    public static void getRefundList(Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.DLISTCENTER)
                .addParams("AppType", "RefundList_")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsonlist")
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }


    /**
     * 获取我的收藏列表，商品收藏列表
     *
     * @param error cmd=Modification   //方法名
     *              out=jsonlist       //数据传输方式
     *              type=3            //type固定传3，不能传别的
     *              sid=ppp          //收藏的sid
     */
    public static void removeMyCollectionGoods(String sid, Consumer<Object> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.REMOVE_COLLECTION)
                .addParams("cmd", "Modification")
                .addParams("sid", sid)
                .addParams("type", "3")
                .addParams("out", "jsonlist")
                .setObject(true)
                .setClass(Object.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的收益
     *
     * @param error cmd=MyIncome    //方法名固定传MyIncome
     *              token=000       //token身份验证
     *              outdata=jsondata //数据输出方式
     */
    public static void getYidlds(Consumer<Object> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.SIMPLE_PLAN_CMD)
                .addParams("cmd", "MyIncome")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsondata")
                .setObject(true)
                .setClass(Object.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的收益记录列表
     *
     * @param error AppType=EarningsList_  //EarningsList_固定传
     *              token=00000     //用户token令牌
     *              outdata=jsonlist  //数据输出方式
     */
    public static void getEarningsList(Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.DLISTCENTER)
                .addParams("AppType", "EarningsList_")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsonlist")
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的邀请列表
     *
     * @param error AppType=Myinvite_  //固定传Myinvite_
     *              token=000          //传用户身份token
     *              outdata=jsonlist   //数据输出方式
     */
    public static void getMyinviteList(Consumer<SubModelList> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.DLISTCENTER)
                .addParams("AppType", "Myinvite_")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsonlist")
                .setObject(true)
                .setClass(SubModelList.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 添加订单
     *
     * @param error 参数名=参数值    //是否必选项及意义描述
     *              cmd=Orderadd         //指定某个方法
     *              outdata=jsonlist     //排序方式
     *              Addresid=888         //地址的sid
     *              token=00             //token验证
     *              content=[{sid:'商品sid',serve:'上门服务',num:'商品数量',stadarry:'属性sid集合,传字符数组',argument:'属性的中文名称'}]         //商品参数拼接成json字符的数组
     *              paytype=alipay   //选择支付方式，alipay支付宝，weixin微信支付
     */
    public static void setOrder(String payType, String content, String addresId, Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.SIMPLE_PLAN_CMD)
                .addParams("cmd", "Orderadd")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsonlist")
                .addParams("Addresid", addresId)
                .addParams("content", content)
                .addParams("paytype", payType)
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的提现列表
     * AppType=WithdrawalList_  //固定传WithdrawalList_
     * token=0000              //传用户的token
     * outdata=jsonlist         //数据输出方式
     * Type=0       //0:一月，1：三月，2：半年 不传：一个星期
     */
    public static void getwithDrawList(String type, Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.DLISTCENTER)
                .addParams("AppType", "WithdrawalList_")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsonlist")
                .addParams("Type", type)
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 提交 提现
     *
     * @param error cmd=RequestWithdrawal  //固定传RequestWithdrawal
     *              token=000           //用户token身份
     *              money=9             //提现金额
     *              type=0      //提现方式  0微信，1支付宝，2银行卡
     *              account=kkkk   //收款账号
     */
    public static void toWithDraw(String money, String type, String bank, Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.SIMPLE_PLAN_CMD)
                .addParams("cmd", "RequestWithdrawal")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("money", money)
                .addParams("type", type)
                .addParams("account", bank)
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的粉丝列表
     * AppType=Myfans_         //固定
     * token=111
     * outdata=jsonlist
     */
    public static void getMyFansList(Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.DLISTCENTER)
                .addParams("AppType", "Myfans_")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("outdata", "jsonlist")
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 添加商品评价
     * AppType=Myfans_         //固定
     * token=111
     * outdata=jsonlist TODO
     */
    public static void pushScore(Consumer<JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.AMAPI_AGMESSAGE)
                .addParams("cmd", "adit")
                .addParams("token", HomeGoodsApp.getToken())
                .setObject(true)
                .setClass(JSONObject.class)
                .create();
        HttpManager.getInstance().postUrlForQuery(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 获取我的粉丝列表
     * cmd=ApplySales         //固定传ApplySales
     * token=aaaa             //用户token
     * ordersid=aabb          //订单的sid
     */
    public static void getApplySales(String ordersid, Consumer<String> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.REMOVE_COLLECTION)
                .addParams("cmd", "ApplySales")
                .addParams("token", HomeGoodsApp.getToken())
                .addParams("ordersid", ordersid)
                .setObject(true)
                .setClass(String.class)
                .create();
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    /**
     * 发送短信验证码(已经注册后使用)
     *
     * @param phone
     * @return
     */
    public static void getCaptchaForForgetPassWord(String phone, Consumer<com.alibaba.fastjson.JSONObject> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(Constant.SERVER_URL + Constant.CLIENT_SEND_VERIFYCODE)
//                .addParams("telphone", phone) //正式参数，验证码使用/sso/send/verifycode参数时用
                .addParams("telephone", phone)  //临时参数，验证码使用/sso/authcode参数时使用
//                .addParams("job", "fpw")
                .setObject(true)
                .setClass(com.alibaba.fastjson.JSONObject.class)
                .create();
//        HttpManager.getInstance().post(command, new ActionSubscriber<>(consumer, error, null));
        HttpManager.getInstance().get(command, new ActionSubscriber<>(consumer, error, null));
    }

    public static void logout(Consumer<String> consumer, Consumer<Throwable> error) {
        CommandImpl command = new CommandImpl.Builder()
                .setUrl(/*Constant.SERVER_URL + Constant.LOGOUT*/"http://192.168.3.133:8443/logout")
                .setObject(true)
                .setClass(String.class)
                .create();
        HttpManager.getInstance().post(command, new ActionSubscriber<>(consumer, error, null));
    }

}
